<?php 
class GoodsAttrModel extends Model{
	public $table="goodsattr";

	public function addData($gid){
			
			foreach (Q('post.attr',array()) as $taid => $value) {
				
				//如果不为空才插入
				if($value){
					$data = array(
						'gavalue' => $value,
						'typeproperty_id' => $taid,
						'goods_gid' => $gid
					);
					$this->add($data);
				}
			}
			
			$spec = isset($_POST['spec']) ? $_POST['spec'] : array();
			//插入规格
			
			foreach ($spec as $taid => $v) {
				//这个地方为什么要循环，看打印的post
				foreach ($v['value'] as $key => $value) {
					$data = array(
						'gavalue' => $value,
						'add-price' => $v['price'][$key],
						'typeproperty_id' => $taid,
						//因为没有添加商品，所以模拟商品id为1
						'goods_gid' => $gid
					);
					$this->add($data);
				}

        	
        }
			
	
	}
	public function updateData($gid){
//		p($gid);die;
//		先删除商品属性表中的相关数据
		$this->where("goods_gid={$gid}")->delete();
		      
				foreach (Q('post.attr',array()) as $taid => $value) {
				
				//如果不为空才插入
				if($value){
					$data = array(
						'gavalue' => $value,
						'typeproperty_id' => $taid,
						'goods_gid' => $gid
					);
					$this->add($data);
				}
			}
			
			$spec = isset($_POST['spec']) ? $_POST['spec'] : array();
			//插入规格
			
			foreach ($spec as $taid => $v) {
				//这个地方为什么要循环，看打印的post
				foreach ($v['value'] as $key => $value) {
					$data = array(
						'gavalue' => $value,
						'add-price' => $v['price'][$key],
						'typeproperty_id' => $taid,
						//因为没有添加商品，所以模拟商品id为1
						'goods_gid' => $gid
					);
					$this->add($data);
				}
        	
		
		
	}
	
}
}

	
?>